package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.BlogMapper;
import edu.dayu.mapper.TypeMapper;
import edu.dayu.pojo.Paging;
import edu.dayu.pojo.Type;
import edu.dayu.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageInfo<Type> listType(Paging paging) {
        if (paging.getPageNum()<1){
            paging.setPageNum(1);
        }
        PageHelper.startPage(paging.getPageNum(),paging.getPageSize());
        List<Type> typeList = typeMapper.listType();
        PageInfo<Type> pageInfo = new PageInfo<>(typeList);
        return pageInfo;
    }

    @Override
    public List<Type> listType() {
        return typeMapper.listType();
    }

    @Override
    public Type getType(int id) {
        return typeMapper.getType(id);
    }

    @Override
    public Boolean saveType(Type type) {
        Type typeName = typeMapper.getTypeName(type);
        if (typeName==null)
        return typeMapper.saveType(type);
        else {
            return false;
        }
    }

    @Override
    public Boolean updateType(Type type) {
        Type typeName = typeMapper.getTypeName(type);
        if (typeName==null)
        return typeMapper.updateType(type);
        else {
            return false;
        }
    }

    @Override
    public void deleteType(int id) {
        typeMapper.deleteType(id);
    }

    @Override
    public List<Type> countTopThree() {
        List<Type> types = typeMapper.listType();
        int[] intlist = blogMapper.listBlogPublished();
        int[] ints = new int[intlist.length];
        for (int i=0;i<intlist.length;i++){
            ints[i]=blogMapper.getBlogTypeId(intlist[i]);
        }
        List<Type> typeList = new ArrayList<>();
        int a[] = new int[100];
        int t = 0;
        for (Type type : types) {
            Integer integer = typeMapper.countTopThree(type.getId());
            for (int i =0;i<ints.length;i++){
                if (ints[i]==type.getId())
                    integer--;
            }
            a[t++]=integer!=null? integer:0;
        }
        int out[] = new int [3];
        int input[] = new int [3];
        for (int i =0 ; i<3;i++){
            int k =0;
            for (int j =0;j<t;j++){
                if (k<a[j]){
                    k=a[j];
                    out[i]=j;
                }
            }
            input[i]=a[out[i]];
            a[out[i]]=0;
        }
        if (out[0]==0)
            out[0]=1;
        for (int i =0 ; i<out.length && out[i]>0 ;i++){
            if (out[0]==1 && i==0)
                out[i]=types.get(out[i]-1).getId();
            else
                out[i]=types.get(out[i]).getId();
            Type type = typeMapper.getType(out[i]);
            type.setCount(input[i]);
            typeList.add(type);
        }
        return typeList;
    }

    @Override
    public Integer countType() {
        return typeMapper.countType();
    }

    @Override
    public List<Type> countTopAll() {
        List<Type> types = typeMapper.listType();
        int[] intlist = blogMapper.listBlogPublished();
        int[] ints = new int[intlist.length];
        for (int i=0;i<intlist.length;i++){
            ints[i]=blogMapper.getBlogTypeId(intlist[i]);
        }
        List<Type> typeList = new ArrayList<>();
        int a[] = new int[100];
        int t = 0;
        for (Type type : types) {
            Integer integer = typeMapper.countTopThree(type.getId());
            for (int i =0;i<ints.length;i++){
                if (ints[i]==type.getId())
                    integer--;
            }
            a[t++]=integer!=null? integer:0;
        }
        int out[] = new int [100];
        int input[] = new int [100];
        for (int i =0 ; i<t;i++){
            int k =-1;
            for (int j =0;j<t;j++){
                if (k<a[j]){
                    k=a[j];
                    out[i]=j;
                }
            }
            input[i]=a[out[i]];
            a[out[i]]=-1;
        }
        for (int i =0 ; i<t;i++){
            out[i]=types.get(out[i]).getId();
            Type type = typeMapper.getType(out[i]);
            type.setCount(input[i]);
            typeList.add(type);
        }
        return typeList;
    }
}
